import Vue from "vue";
import Router from "vue-router";
import { store } from "../store/store";

Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    { path: "/", component: resolve => require(["@/pages/login"], resolve) },
    {
      path: "/index",
      component: resolve => require(["@/pages/index"], resolve),
      redirect: "/indicatorsList",
      children: [
        // 仪表盘
        {
          path: "/dashboard",
          component: resolve => require(["@/components/dashboard"], resolve)
        },
        // 数据分析
        // {
        //   path: "/dataAnalysis",
        //   component: resolve => require(["@/components/dataAnalysis"], resolve)
        // },
        // 目标任务计划采集
        {
          path: "/indicatorsList",
          component: resolve =>
            require(["@/components/indicatorsAdm/indicatorsList"], resolve)
        },
        // 目标任务计划审核
        {
          path: "/indicatorsListAudit",
          component: resolve =>
            require(["@/components/indicatorsAdm/indicatorsListAudit"], resolve)
        },

        {
          path: "/indicatorsListAudit_2",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/indicatorsListAudit_2"
            ], resolve),
          meta: {
            name: "分解详情"
          }
        },
        {
          path: "/indicatorsListAudit_3",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/indicatorsListAudit_3"
            ], resolve),
          meta: {
            name: "目标配合单位"
          }
        },
        // 指标列表
        {
          path: "/taskScore",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/component/taskScore"
            ], resolve),

          meta: {
            name: "分解详情"
          }
        },
        // 配合单位列表
        {
          path: "/cooperateList",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/component/cooperateList"
            ], resolve),
          meta: {
            name: "配合单位列表"
          }
        },

        // 指标录入
        {
          path: "/RecordList",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/component/RecordList"
            ], resolve)
        },
        {
          path: "/inboyList",
          component: resolve =>
            require(["@/components/indicatorsAdm/component/inboyList"], resolve)
        },
        // 指标进度填报
        {
          path: "/targetList",
          component: resolve =>
            require(["@/components/indicatorsAdm/targetList"], resolve)
        },
        // 指标进度审核
        {
          path: "/targetListAudit",
          component: resolve =>
            require(["@/components/indicatorsAdm/targetListAudit"], resolve)
        },
        // 指标进度审核 2页面
        {
          path: "/targetListAudit_2",
          component: resolve =>
            require(["@/components/indicatorsAdm/targetListAudit_2"], resolve),
          meta: {
            name: "指标进度审核列表"
          }
        },
        // 指标进度审核 2页面
        {
          path: "/targetListAudit_3",
          component: resolve =>
            require(["@/components/indicatorsAdm/targetListAudit_3"], resolve),
          meta: {
            name: "单位进度列表"
          }
        },
        // 亮灯设置页面
        {
          path: "/targetListAudit_4",
          component: resolve =>
            require(["@/components/indicatorsAdm/targetListAudit_4"], resolve),
          meta: {
            name: "亮灯设置"
          }
        },

        // 评分汇总
        {
          path: "/planSummary",
          component: resolve =>
            require(["@/components/indicatorsAdm/planSummary"], resolve)
        },
        {
          path: "/planSummary2",
          component: resolve =>
            require(["@/components/indicatorsAdm/planSummary2"], resolve),
          meta: {
            name: "配合单位"
          }
        },
        // 目标任务综合查询
        {
          path: "/depPlanSummary",
          component: resolve =>
            require(["@/components/indicatorsAdm/depPlanSummary"], resolve)
        },
        {
          path: "/depPlanSummary2",
          component: resolve =>
            require(["@/components/indicatorsAdm/depPlanSummary2"], resolve),
          meta: {
            name: "配合单位"
          }
        },

        {
          path: "/targetListboy",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/component/targetListboy"
            ], resolve),
          meta: {
            name: "指标进度列表"
          }
        },
        {
          path: "/targetListboyTow",
          component: resolve =>
            require([
              "@/components/indicatorsAdm/component/targetListboyTow"
            ], resolve),
          meta: {
            name: "配合责任单位指标进度列表"
          }
        },

        // 指标完成率
        {
          path: "/indicatorsScore",
          component: resolve =>
            require(["@/components/evaluationScore/indicatorsScore"], resolve)
        },
        // 得分汇总
        {
          path: "/Scoresummary",
          component: resolve =>
            require(["@/components/evaluationScore/Scoresummary"], resolve)
        },
        // 数据录入
        {
          path: "/dataentry",
          component: resolve =>
            require(["@/components/evaluationScore/dataentry"], resolve)
        },
        // 日常台账下面的 -》其他
        {
          path: "/ScoreElse",
          component: resolve =>
            require(["@/components/evaluationScore/ScoreElse"], resolve)
        },
        {
          path: "/indicatorTaskPlan",
          component: resolve =>
            require(["@/components/evaluationScore/indicatorTaskPlan"], resolve)
        },
        {
          path: "/indicatorDepPlan",
          component: resolve =>
            require(["@/components/evaluationScore/indicatorDepPlan"], resolve)
        },
        // 配合单位打分
        {
          path: "/tenTaskScore",
          component: resolve =>
            require(["@/components/evaluationScore/tenTaskScore"], resolve)
        },
        // 考核打分审核
        {
          path: "/tenTaskScoreAudit",
          component: resolve =>
            require(["@/components/evaluationScore/tenTaskScoreAudit"], resolve)
        },
        {
          path: "/tenTaskScoreAuditPage",
          component: resolve =>
            require([
              "@/components/evaluationScore/tenTaskScoreAuditPage"
            ], resolve)
        },
        {
          path: "/unitScore",
          component: resolve =>
            require(["@/components/evaluationScore/unitScore"], resolve)
        },
        // 配合单位打分2
        {
          path: "/groupingScore",
          component: resolve =>
            require([
              "@/components/evaluationScore/component/groupingScore"
            ], resolve)
        },
        // 考核单位评分2
        {
          path: "/unitboyScore",
          component: resolve =>
            require([
              "@/components/evaluationScore/component/unitboyScore"
            ], resolve)
        },

        // 目标任务打分2
        // { path: '/taskScoreBoy', component: resolve => require(['@/components/evaluationScore/component/taskScoreBoy'], resolve)},
        // 各方面评价
        // 党政一把手评价
        {
          path: "/unitEva",
          component: resolve =>
            require(["@/components/allRoundScore/unitEva"], resolve),
            meta: {
              name: "党政一把手评价"
            }
        },
        // 两代表一委员
        {
          path: "/threeRepOneMen",
          component: resolve =>
            require(["@/components/allRoundScore/threeRepOneMen"], resolve),
            meta: {
              name: "公众评价"
            }
        },
        // 
        {
          path: "/twoRepOneMen",
          component: resolve =>
            require(["@/components/allRoundScore/twoRepOneMen"], resolve),
            meta: {
              name: "两代表一委员"
            }
        },
        // 成员单位评价 Evaluation
        {
          path: "/Evaluation",
          component: resolve =>
            require(["@/components/allRoundScore/Evaluation"], resolve)
        },
        // 党政一把手评价
        {
          path: "/amember",
          component: resolve =>
            require(["@/components/allRoundScore/amember"], resolve)
        },
        // 一线督导核验
        {
          path: "/supervisor",
          component: resolve =>
            require(["@/components/supervisor/supervisor"], resolve)
        },
        {
          path: "/newslism",
          component: resolve =>
            require(["@/components/supervisor/newslism"], resolve)
        },

         // 其他
         {
          path: "/dataElse",
          component: resolve =>
            require(["@/components/allRoundScore/dataElse"], resolve)
        },
        {
          path: "/fileInfo",
          component: resolve =>
            require(["@/components/fileAdm/fileInfo"], resolve)
        },
        {
          path: "/workfileAdm",
          component: resolve =>
            require(["@/components/fileAdm/workfileAdm"], resolve)
        },
        {
          path: "/workState",
          component: resolve =>
          require(["@/components/fileAdm/workState"], resolve)
        },
        {
          path: "/workLookData",
          component: resolve =>
          require(["@/components/fileAdm/workLookData"], resolve)
        },
        // 数据分析
        {
          path: "/dataAnalysysCharts",
          component: resolve =>
            require(["@/components/dataAnalysys/dataAnalysysCharts"], resolve)
        },
        // 考核排名分析
        {
          path: "/dataAnalysysCharts_2",
          component: resolve =>
            require(["@/components/dataAnalysys/dataAnalysysCharts_2"], resolve)
        },
        // 重点工作排名
        {
          path: "/dataAnalysysCharts_3",
          component: resolve =>
            require(["@/components/dataAnalysys/dataAnalysysCharts_3"], resolve)
        },
        // 其他
        {
          path: "/dataAnalysysCharts_4",
          component: resolve =>
            require(["@/components/dataAnalysys/dataAnalysysCharts_4"], resolve)
        },
        // 重点工作打分排名
        {
          path: "/dataAnalysysCharts_6",
          component: resolve =>
            require(["@/components/dataAnalysys/dataAnalysysCharts_6"], resolve)
          },
        // 责任单位考核分析
        {
          path: "/dataAnalysysCharts_5",
          component: resolve =>
            require(["@/components/dataAnalysys/dataAnalysysCharts_5"], resolve)
        },
        // 目标任务查重
        {
          path: "/targetTaskSet",
          component: resolve =>
            require(["@/components/taskDataSet/targetTaskSet"], resolve)
        },
        // 考核亮灯配置
        {
          path: "/scoreLampInfo",
          component: resolve =>
            require(["@/components/systemAdm/scoreLampInfo"], resolve)
        },
        // 部门信息
        {
          path: "/deptmentInfo",
          component: resolve =>
            require(["@/components/systemAdm/deptmentInfo"], resolve)
        },
        // 用户信息
        {
          path: "/userInfo",
          component: resolve =>
            require(["@/components/systemAdm/userInfo"], resolve)
        },
        // 权限信息
        {
          path: "/permissionsInfo",
          component: resolve =>
            require(["@/components/systemAdm/permissionsInfo"], resolve)
        },
        // 日志信息
        {
          path: "/logInfo",
          component: resolve =>
            require(["@/components/systemAdm/logInfo"], resolve)
        },
        // 任务信息
        {
          path: "/taskInfo",
          component: resolve =>
            require(["@/components/systemAdm/taskInfo"], resolve)
        },
        // 区域管理
        // { path: '/areaInfo', component: resolve => require(['@/components/systemAdm/areaInfo'], resolve)},
        // 群组管理
        {
          path: "/groupInfo",
          component: resolve =>
            require(["@/components/systemAdm/groupInfo"], resolve)
        },
        // 扣分项管理
        {
          path: "/listInfo",
          component: resolve =>
            require(["@/components/systemAdm/listInfo"], resolve)
        },
        // 负面清单
        {
          path: "/negativeList",
          component: resolve =>
            require(["@/components/addSubAdm/negativeList"], resolve)
        },
        // 日常工作台账
        {
          path: "/workLedger",
          component: resolve =>
            require(["@/components/addSubAdm/workLedger"], resolve)
        },
        // 加减分项
        {
          path: "/addsubData",
          component: resolve =>
            require(["@/components/addSubAdm/addsubData"], resolve)
        }
      ]
    }
  ]
});
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

function selectPath(key) {
  let str = key.substr(1);
  if (str.indexOf("?") != -1) {
    str = str.split("?")[0];
  }
  let array = JSON.parse(localStorage.getItem("permissions"));
  let _text = "";
  let _index = "";
  let navigation = [];
  array.forEach(item => {
    if (item.path == str) {
      _text = item.permissionName;
      _index = 1;
      navigation = [];
      navigation.push({
        name: item.permissionName,
        path: ""
      });
    } else {
      if (item.childrens.length != 0) {
        item.childrens.forEach(i => {
          if (i.path == str) {
            navigation.push(
              {
                name: item.permissionName,
                path: `/${i.path}`
              },
              {
                name: i.permissionName,
                path: `/${i.path}`
              }
            );
            _text = i.permissionName;
            _index = 2;
            sessionStorage.setItem("navigation", JSON.stringify(navigation));
          }
        });
      }
    }
  });
  // 代表没有在路由重找到
  if (!_text) {
    let str = key;
    if (str.indexOf("?") != -1) {
      str = str.split("?")[0];
    }
    let _router = router.options.routes[1].children;
    _router.forEach((item, index) => {
      if (item.path == str) {
        if (item.meta) {
          navigation.push({
            name: item.meta.name,
            path: key
          });
          let arr = JSON.parse(sessionStorage.getItem("navigation"));
          let _index = null;
          arr.forEach((item, index) => {
            if (item.name == navigation[0].name) {
              _index = index + 1;
            }
          });
          if (_index == null) {
            navigation = [...arr, ...navigation];
          } else {
            navigation = arr.splice(_index);
            navigation = arr;
          }
        }
      }
    });
  }
  if (navigation.length == 0) {
    navigation = JSON.parse(sessionStorage.getItem("navigation"));
  }

  sessionStorage.setItem("navigation", JSON.stringify(navigation));
  store.commit("btn_navigation", navigation);
}
// 解决乱码问题
function getUrlVars() {
  var vars = [],
    hash;
  var hashes = window.location.href
    .slice(window.location.href.indexOf("?") + 1)
    .split("&");
  for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split("=");
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}

//路由守卫
router.beforeEach((to, from, next) => {
  let arr = JSON.parse(localStorage.getItem("permissions"));
  if (arr != null) {
    selectPath(to.fullPath);
  }
  const isLogin = localStorage.getItem("token") ? true : false;
  if (to.path == "/") {
    next();
  } else {
    isLogin ? next() : next("/");
  }
});

export default router;
